package com.zcup.designer.contribute.iterator;

/*
    具体迭代器类,继承Iterator
 */
public class ConcreteIterator extends Iterator{
    //定义一个聚集对象
    private ConcreteAggregate aggregate;
    private int current = 0;

    public ConcreteIterator(ConcreteAggregate aggregate){
        this.aggregate = aggregate;
    }

    private ConcreteIterator(){}

    @Override
    public Object first() {
        return aggregate.getItem(0);
    }

    @Override
    public Object next() {
        Object result = null;
        if (current < aggregate.getCount()){
            result = aggregate.getItem(current);
        }
        current ++;
        return result;
    }

    @Override
    public boolean isEnd() {
        return current >= aggregate.getCount();
    }

    @Override
    public Object currentItem() {
        return aggregate.getItem(current);
    }
}
